% 等量代换（简单方程)
clear;
% 页数
pagenum    = 4;
% 生成文档
m = 3;
n = 2;
GenDocument([], "mydoc", pagenum, m, n);


function drawEquation1(~)
% \diamondsuit
% \spadesuit	
% \clubsuit
% \heartsuit
    fsize = 9;
    x = randi(9);
    a = randi(5);
    b = randi(9);
    d = randi(5);
    e = randi(9);
    c = a*x+b;
    A = "\diamondsuit";
    B = "\spadesuit";
    s1 = strtimes(A, a);
    s2 = [' + ',num2str(b), ' = ', num2str(c)];
    s  = strcat(s1,s2);
    text(0, 0.5, s,'FontSize',fsize);
    s1 = strtimes(A, d);
    s2 = [' + ',num2str(e), ' = '];
    s  = strcat(s1,s2,B);
    text(0, 0.3, s,'FontSize',fsize);
    axis off;
end
function drawEquation2(~)
% \diamondsuit
% \spadesuit	
% \clubsuit
% \heartsuit
    fsize = 9;
    a = randi(4);
    b = randi(4);
    c = b + randi(3);
    x = randi(9);
    y = randi(9);
    d = a*x+b*y;
    e = a*x+c*y;
    A = "\diamondsuit";
    B = "\spadesuit";
    s1 = strtimes(A, a);
    s2 = strtimes(B, b);
    s3 = [' = ', num2str(d)];
    s  = strcat(s1,"+",s2,s3);
    text(0, 0.5, s,'FontSize',fsize);
    s1 = strtimes(A, a);
    s2 = strtimes(B, c);
    s3 = [' = ', num2str(e)];
    s  = strcat(s1,"+",s2,s3);
    text(0, 0.3, s,'FontSize',fsize);
    axis off;
end

function drawEquation3(~)
% \diamondsuit
% \spadesuit	
% \clubsuit
% \heartsuit
    fsize = 9;
    a = randi(2);
    b = randi(2);
    c = 2*b + randi(2);
    x = randi(9);
    y = randi(9);
    d = a*x+b*y;
    e = 2*a*x+c*y;
    A = "\diamondsuit";
    B = "\spadesuit";
    s1 = strtimes(A, a);
    s2 = strtimes(B, b);
    s3 = [' = ', num2str(d)];
    s  = strcat(s1,"+",s2,s3);
    text(0, 0.5, s,'FontSize',fsize);
    s1 = strtimes(A, 2*a);
    s2 = strtimes(B, c);
    s3 = [' = ', num2str(e)];
    s  = strcat(s1,"+",s2,s3);
    text(0, 0.3, s,'FontSize',fsize);
    axis off;
end
function s2 = strtimes(s, n)
    s2 = "";
    for i = 1:n
        s2 = strcat(s2, s);
    end
end

% 生成方程
function drawEquation(ax)
    idx    = randi([1,3]);
    funTab = {@drawEquation1,@drawEquation2,@drawEquation3};
    func   = funTab{idx};
    func(ax);
end

% 绘制函数
function drawFunction(ax)
    drawEquation(ax);
end

%% 以下是基础函数

% 生成图片
function GenerateImages(num, m, n)
    for i = 1:num
        a = subplot(m,n,1);
        cla;
        drawFunction(a);
        F = getframe(a);
        I = frame2im(F);
        imwrite(I,['p',num2str(i),'.png']);
    end
end

% 删除图片
function deletePngs(num)
    for i = 1:num
        delete(['p',num2str(i),'.png']);
    end
end

% 生成文档
function GenDocument(~, filename, pagenum, m, n)
% 参数
% problemstr 题目列表
% filename 文件名
% pagenum 页数
import mlreportgen.dom.*
d = Document(filename,"docx");
open(d);
cnt   = 1;
GenerateImages(12*pagenum, m, n);
tableDatas = cell(4,3,pagenum);
pidx = 0;
for i = 1:pagenum
    for j = 1:4
        for k = 1:3
            pidx = pidx + 1;
            tableDatas{j, k, i} = Image(['p',num2str(pidx),'.png']);
        end
    end
end
for i = 1:pagenum
    % 表格式
    tableStyles       = { ColSep("solid"), RowSep("solid"), Border("solid"), Width("100%"), RowHeight('140pt','atleast'), FontSize("12pt")};
    % 表头格式
    tableHeaderStyles = { BackgroundColor("lightgray"), Bold(true), FontSize("16pt"),RowHeight('20pt')};
    % 表头名称
    headerLabels      = ["开始：___","结束： ____" ,"得分： ____"];
    tableData = cell(4,3);
    for idx = 1:12
        [a,b] = ind2sub([4,3],idx);
        tableData{a,b} = tableDatas{a,b,i};
    end
    cnt       = cnt + 12;
    cellTbl   = FormalTable(headerLabels,tableData);
    cellTbl.Style = [cellTbl.Style, tableStyles];
    cellTbl.Header.Style = [cellTbl.Header.Style, tableHeaderStyles];
    cellTbl.TableEntriesInnerMargin = "2pt";
    append(d,cellTbl);
    if i ~= pagenum
        br = PageBreak();
        append(d,br);
    end
end
close(d);
deletePngs(12*pagenum);
end